<?php
/**
 * 火车头采集发布接口 接收数据
 */
defined('IN_PHPCMS') or exit('No permission resources.');
define('HASH', SHA1('abc'));

if(HASH != $_GET['hash']) exit('验证错误');
if(!$_GET['siteid']) exit("站点ID不存在");

if($_POST){
    $info['catid'] = isset($_POST['catid']) ? intval($_POST['catid']): exit("栏目ID不存在");
    $info['title'] = isset($_POST['title']) ? trim($_POST['title']) : exit('标题不能为空');
    if($_GET['act'] == 'picture'){ //处理组图文章
        $info['pictureurls'] = 1;
        $info['pictureurls_url'] = isset($_POST['content']) ? spider_photos($_POST['content']) : '';
        $info['content'] = '';
    }else{
        $info['content'] = isset($_POST['content']) ? new_stripslashes(close_tags(trim($_POST['content']))) : exit('内容不能为空');
    }
    $info['status'] = 99;
    $info['keywords'] = !empty($_POST['keywords']) ? $_POST['keywords'] : '';
    //$info['description'] = !empty($_POST['description']) ? $_POST['description'] : str_cut(str_replace(array("\r\n","\t",'[page]','[/page]','&ldquo;','&rdquo;','&nbsp;'), '', strip_tags($info['content'])),200);
    $info['username'] = isset($_POST['username']) ? trim($_POST['username']) : '';
    $info['inputtime'] = isset($_POST['date']) ? trim($_POST['date']) : '';
    $info['paginationtype'] = strpos($info['content'], '[page]') > 0 ? 2 : 0; //设定文章内容分页为手动
    $info['thumb'] = isset($_POST['thumb']) ? trim($_POST['thumb']) : '';
    $info['copyfrom'] = isset($_POST['copyfrom']) ? trim($_POST['copyfrom']).'|0' : '|0';
    //采用CURL模拟POST再次提交，不这么提交，组图发不了！需要服务器支持CURL！
    $data = curl_post('http://toutiao.youth.cn/api.php?op=collection_post&siteid='.$_GET['siteid'].'&act='.$_GET['act'].'&hash='.$_GET['hash'], $info);
    exit($data);
}else{
    $categorys = getcache('category_content_'.$_GET['siteid'],'commons');
    $str = '';
    foreach($categorys as $k=>$v){
        if(!$v['type'] && $v['child'] == 0 && $v['modelid'] == $_GET['modelid']) $str .=  $v['catid'].'-'.$v['catname'].'|';
    }
    exit($str);
}

function spider_photos($str){
    $array = array();
    preg_match_all('/<img[^>]*src=[\'"]?([^>\'"\s]*)[\'"]?[^>]*>/i', $str, $out);
    if(isset($out[1])) foreach($out[1] as $key=>$pic) {
        $array[$key] = $pic;
    }
    return $array;
}

function curl_post($url, $post){
    $options = array(
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HEADER         => false,
        CURLOPT_POST           => true,
        CURLOPT_POSTFIELDS     => http_build_query($post),
    );

    $ch = curl_init($url);
    curl_setopt_array($ch, $options);
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}

function close_tags($html){
    // strip fraction of open or close tag from end (e.g. if we take first x characters, we might cut off a tag at the end!)
    $html = preg_replace('/<[^>]*$/','',$html); // ending with fraction of open tag
    // put open tags into an array
    preg_match_all('#<([a-z]+)(?: .*)?(?<![/|/ ])>#iU', $html, $result);
    $opentags = $result[1];
    // put all closed tags into an array
    preg_match_all('#</([a-z]+)>#iU', $html, $result);
    $closetags = $result[1];
    $len_opened = count($opentags);
    // if all tags are closed, we can return
    if(count($closetags) == $len_opened){
        return $html;
    }
    // close tags in reverse order that they were opened
    $opentags = array_reverse($opentags);
    // self closing tags
    $sc = array('br','input','img','hr','meta','link');
    // ,'frame','iframe','param','area','base','basefont','col'
    // should not skip tags that can have content inside!
    for($i=0; $i < $len_opened; $i++){
        $ot = strtolower($opentags[$i]);
        if(!in_array($opentags[$i], $closetags) && !in_array($ot,$sc)){
            $html .= '</'.$opentags[$i].'>';
        }else{
            unset($closetags[array_search($opentags[$i], $closetags)]);
        }
    }
    return $html;
}
?>